function createUserFormWin(userId) {
	var uid = userId;

	var roleDs = new Ext.data.Store({
		proxy : new Ext.data.HttpProxy({
			url : DISPATCH_URL
		}),
		reader : new Ext.data.JsonReader({
			root : 'roles',
			id : 'id',
			fields : [ {
				name : 'id'
			}, {
				name : 'name'
			}, {
				name : 'description'
			} ]
		})
	});
	roleDs.baseParams = {
		'cmd' : 'role',
		'action' : 'find'
	};

	var roleSm = new Ext.grid.CheckboxSelectionModel();
	var roleCm = new Ext.grid.ColumnModel([ new Ext.grid.RowNumberer(), roleSm,
			{
				header : TXT_role_name,
				dataIndex : 'name',
				width : 150
			}, {
				header : TXT_role_description,
				dataIndex : 'description',
				width : 200
			} ]);

	var roleGrid = new Ext.grid.GridPanel({
		border : true,
		region : 'center',
		ds : roleDs,
		cm : roleCm,
		sm : roleSm,
		stripeRows : true,
		loadMask : true,
		title : TXT_user_role
	});
	roleDs.load();

	var siteDs = new Ext.data.Store({
		proxy : new Ext.data.HttpProxy({
			url : DISPATCH_URL
		}),
		reader : new Ext.data.JsonReader({
			root : 'sites',
			id : 'id',
			fields : [ {
				name : 'id'
			}, {
				name : 'name'
			} ]
		})
	});
	siteDs.baseParams = {
		'cmd' : 'site',
		'action' : 'find'
	};
	siteDs.on('loadexception', function() {
		// Ext.MessageBox.alert(TXT_common_hint,TXT_common_access_fail);
	});
	var siteCombo = new Ext.form.ComboBox({
		// tpl: '<tpl for="."><div ext:qtip="{bic}. {name}"
		// class="x-combo-list-item">{bic}</div></tpl>',
		fieldLabel : TXT_user_site,
		id : 'site',
		name : '所属驻地',
		displayField : 'name',
		valueField : 'id',
		typeAhead : true,
		triggerAction : 'all',
		selectOnFocus : true,
		triggerClass : 'x-form-trigger2',
		store : siteDs,
		lazyInit : false,
		editable : false,
		allowBlank : false
	});

	siteDs.load();

	var userForm = new Ext.FormPanel({
		labelAlign : 'left',
		region : 'north',
		labelWidth : 60,
		height : 125,
		margins : '0 0 2 0',
		cmargins : '0 0 2 0',
		// bodyStyle:'margin-right:5px',
		frame : true,
		items : [ {
			layout : 'column',
			items : [ {
				columnWidth : .5,
				layout : 'form',
				defaultType : 'textfield',
				defaults : {
					anchor : '85%'
				},
				items : [ {
					fieldLabel : TXT_user_username,
					id : 'username',
					name : 'username',
					allowBlank : false
				}, {
					fieldLabel : TXT_user_phone,
					id : 'phone',
					name : 'phone',
					allowBlank : true
				}, {
					fieldLabel : TXT_user_login_name,
					id : 'loginName',
					name : 'loginName',
					allowBlank : false
				}, {
					fieldLabel : TXT_user_password,
					inputType : 'password',
					id : 'password',
					name : 'password',
					allowBlank : false
				} ]
			}, {
				columnWidth : .5,
				layout : 'form',
				defaultType : 'textfield',
				defaults : {
					anchor : '85%'
				},
				items : [ siteCombo, {
					fieldLabel : TXT_user_email,
					id : 'email',
					vtype : 'email',
					name : 'email',
					allowBlank : true
				}, {
					xtype : 'checkbox',
					fieldLabel : TXT_user_operational,
					boxLabel : 'Yes',
					checked : true,
					id : 'operational',
					name : 'operational'
				}, {
					fieldLabel : TXT_user_confirm_pwd,
					inputType : 'password',
					id : 'confirmPwd',
					name : 'confirmPwd',
					allowBlank : false
				} ]
			} ]
		} ]
	});

	var win = null;
	if (!win) {
		win = new Ext.Window(
				{
					title : TXT_user_title,
					width : 600,
					height : 400,
					autoScroll : false,
					layout : 'border',
					border : false,
					// closeAction:'hide',
					minimizable : false,
					maximizable : false,
					resizable : false,
					modal : true,
					layoutConfig : {
						animate : false
					},
					items : [ userForm, roleGrid ],
					buttonAlign : 'center',
					buttons : [
							{
								text : TXT_common_OK,
								handler : function() {
									if (!userForm.form.isValid()) {
										return;
									}
									var username = userForm
											.findById('username').getValue();
									var loginName = userForm.findById(
											'loginName').getValue();
									var password = userForm
											.findById('password').getValue();
									var confirmPwd = userForm.findById(
											'confirmPwd').getValue();
									if (password != confirmPwd) {
										Ext.MessageBox.alert(TXT_common_hint,
												TXT_user_confirm_pwd_error);
										return;
									}

									var email = userForm.findById('email')
											.getValue();
									var phone = userForm.findById('phone')
											.getValue();
									var site = userForm.findById('site')
											.getValue();
									var enable = userForm.findById(
											'operational').getValue();
									var roleIds = "";
									var roleRecord = roleSm.getSelections();
									for ( var i = 0; i < roleRecord.length; i++) {
										roleIds += "&roleId="
												+ roleRecord[i].id;

									}

									BestvSearch.showWaitingWin();
									Ext.Ajax
											.request({
												url : DISPATCH_URL,
												method : 'POST',
												params : 'cmd=user&action=save&id='
														+ uid
														+ '&username='
														+ username
														+ '&loginName='
														+ loginName
														+ '&password='
														+ password
														+ '&email='
														+ email
														+ '&site='
														+ site
														+ '&phone='
														+ phone
														+ '&enable='
														+ enable + roleIds,
												success : function(response,
														options) {
													Ext.MessageBox.hide();
													var s = response.responseText;
													if (s == '[error]') {
														Ext.MessageBox
																.alert(
																		TXT_common_hint,
																		TXT_common_access_fail,
																		function() {

																		});
													} else if (s == '[null]') {
														Ext.MessageBox
																.alert(
																		TXT_common_hint,
																		TXT_common_relogin,
																		function() {
																			window.location.href = LOGIN_URL;
																		});
													} else {
														var result = Ext.util.JSON
																.decode(s);
														if (result.result == 'success') {
															win.close();
															reloadUsers();
														} else {
															var msg = '';
															if (result.message == 'loginName')
																msg = TXT_user_need_loginName;
															else if (result.message == 'password')
																msg = TXT_user_need_password;
															else if (result.message == 'site')
																msg = TXT_user_need_site;
															else if (result.message == 'role')
																msg = TXT_user_need_role;
															else
																msg = TXT_user_exist;

															Ext.MessageBox
																	.alert(
																			TXT_common_hint,
																			msg);
														}

													}
												},
												failure : function(response,
														options) {
													Ext.MessageBox.hide();
													Ext.MessageBox
															.alert(
																	TXT_common_hint,
																	TXT_common_access_fail);
												}
											});
								}
							}, {
								text : TXT_common_reset,
								handler : function() {
									userForm.getForm().reset();
								}
							} ]
				});
	}
	win.on('show', function() {
		if (uid != '') {
			Ext.Ajax
					.request({
						url : DISPATCH_URL,
						method : 'POST',
						params : 'cmd=user&action=get&id=' + uid,
						success : function(response, options) {
							var s = response.responseText;
							if (s == '[error]') {
								Ext.MessageBox.alert(TXT_common_hint,
										TXT_common_access_fail);
							} else if (s == '[null]') {
								Ext.MessageBox.alert(TXT_common_hint,
										TXT_common_relogin, function() {
											window.location.href = LOGIN_URL;
										});
							} else {
								var result = Ext.util.JSON.decode(s);
								userForm.findById('username').setValue(
										result.username);
								userForm.findById('phone').setValue(
										result.phone);
								userForm.findById('email').setValue(
										result.email);
								userForm.findById('password').setValue(
										result.password);
								userForm.findById('confirmPwd').setValue(
										result.password);
								userForm.findById('loginName').setValue(
										result.loginName);
								userForm.findById('operational').setValue(
										result.enable);
								// setValue(result.branchId);
								userForm.findById('site').setValue(
										result.siteId);

								// var roleRecords = roleDs.reader.jsonData;
								// for (i = 0; i < roleRecords.roles.length;
								// i++) {
								// var record = roleRecords.roles[i];
								// for (j = 0; j < result.roles.length; j++) {
								// var role = result.roles[j];
								// if (record.id == role.id) {
								// roleSm.selectRow(i,true);
								// }
								// }
								// }
							}
						},
						failure : function(response, options) {
							Ext.MessageBox.alert(TXT_common_hint,
									TXT_common_access_fail);
						}
					});
		}
	});
	win.show();
	roleDs.on('load', function() {
		if (uid != '') {
			Ext.Ajax.request({
				url : DISPATCH_URL,
				method : 'POST',
				params : 'cmd=user&action=get&id=' + uid,
				success : function(response, options) {
					var s = response.responseText;
					if (s == '[error]') {
						Ext.MessageBox.alert(TXT_common_hint,
								TXT_common_access_fail);
					} else if (s == '[null]') {
						Ext.MessageBox.alert(TXT_common_hint,
								TXT_common_relogin, function() {
									window.location.href = LOGIN_URL;
								});
					} else {
						var result = Ext.util.JSON.decode(s);
						var record = new Array(result.roles.length);
						for ( var j = 0; j < result.roles.length; j++) {
							record[j] = roleDs.getById(result.roles[j].id);
						}
						roleSm.selectRecords(record);
					}
				}
			});
		}
	});
}

var ds;

function displayUserPage() {
	Ext.form.Field.prototype.msgTarget = 'side';
	Ext.QuickTips.init();
	// Ext.state.Manager.setProvider(new Ext.state.CookieProvider());
	// var sm = new Ext.grid.CheckboxSelectionModel();
	var sm = new Ext.grid.RowSelectionModel({
		singleSelect : true
	});
	ds = new Ext.data.Store({
		proxy : new Ext.data.HttpProxy({
			url : DISPATCH_URL
		}),
		reader : new Ext.data.JsonReader({
			root : 'users',
			id : 'id',
			fields : [ {
				name : 'id'
			}, {
				name : 'loginName'
			}, {
				name : 'username'
			}, {
				name : 'site'
			}, {
				name : 'email'
			}, {
				name : 'phone'
			}, {
				name : 'enable'
			} ]
		})
	});
	// create payment list grid

	var gridCm = new Ext.grid.ColumnModel(
			[
					new Ext.grid.RowNumberer(),
					{
						header : TXT_user_login_name,
						dataIndex : 'loginName',
						width : 150
					},
					{
						header : TXT_user_username,
						dataIndex : 'username',
						width : 150
					},
					{
						header : TXT_user_site,
						dataIndex : 'site',
						width : 150
					},
					{
						header : TXT_user_email,
						dataIndex : 'email',
						width : 200
					},
					{
						header : TXT_user_phone,
						dataIndex : 'phone',
						width : 200
					},
					{
						header : TXT_user_enable,
						dataIndex : 'enable',
						width : 80,
						renderer : function(value) {
							if (value == false) {
								return "<input type= 'checkbox' disabled='true'/>";
							} else {
								return "<input type= 'checkbox' checked='checked' disabled='true'/>";
							}
						}
					} ]);
	var btnCreate = new Ext.Button({
		text : TXT_common_create,
		tooltip : TXT_common_create,
		iconCls : 'icoAdd'
	});

	var btnEdit = new Ext.Button({
		text : TXT_common_edit,
		tooltip : TXT_common_edit,
		iconCls : 'icoDetail'
	});

	var btnDelete = new Ext.Button({
		text : TXT_common_delete,
		tooltip : TXT_common_delete,
		iconCls : 'icoDel'
	});

	btnCreate.on('click', function() {
		createUserFormWin('');
	});

	btnEdit.on('click', function() {
		if (sm.getCount() == 0) {
			Ext.MessageBox.alert(TXT_common_hint, TXT_user_need);
			return;
		}
		var record = sm.getSelected();
		createUserFormWin(record.id);
	});

	btnDelete
			.on(
					'click',
					function() {
						if (sm.getCount() == 0) {
							Ext.MessageBox
									.alert(TXT_common_hint, TXT_user_need);
							return;
						}
						var record = sm.getSelected();
						Ext.MessageBox
								.confirm(
										TXT_common_hint,
										TXT_user_delete_confirmation,
										function(btn) {
											if (btn == 'yes') {
												BestvSearch.showWaitingWin();
												Ext.Ajax
														.request({
															url : DISPATCH_URL,
															method : 'POST',
															params : 'cmd=user&action=delete&id='
																	+ record.id,
															success : function(
																	response,
																	options) {
																Ext.MessageBox
																		.hide();
																var s = response.responseText;
																if (s == '[error]') {
																	Ext.MessageBox
																			.alert(
																					TXT_common_hint,
																					TXT_common_access_fail);
																} else if (s == '[null]') {
																	Ext.MessageBox
																			.alert(
																					TXT_common_hint,
																					TXT_common_relogin,
																					function() {
																						window.location.href = LOGIN_URL;
																					});
																} else {
																	var result = Ext.util.JSON
																			.decode(s);
																	if (result.result == 'success') {
																		reloadUsers();
																	}
																}
															},
															failure : function(
																	response,
																	options) {
																Ext.MessageBox
																		.hide();
																Ext.MessageBox
																		.alert(
																				TXT_common_hint,
																				TXT_common_access_fail);
															}
														});
											}
										});

					});

	// create the Grid
	var grid = new Ext.grid.GridPanel({
		el : 'grid',
		ds : ds,
		cm : gridCm,
		sm : sm,
		stripeRows : true,
		loadMask : true,
		viewConfig : {
			forceFit : true
		},
		title : TXT_user_title,
		tbar : [ btnCreate, '-', btnEdit, '-', btnDelete ]
	});
	// grid.on('dblclick',function(){
	// if (sm.getCount() == 0) {
	// Ext.MessageBox.alert(TXT_common_hint,TXT_user_need);
	// return;
	// }
	// var record = sm.getSelected();
	// createUserFormWin(record.id);
	// });
	// initMenu();
	ds.baseParams = {
		cmd : 'user',
		action : 'find'
	};
	ds.on('load', function() {
		grid.getSelectionModel().selectFirstRow();
	});
	ds.on('loadexception', function() {
		// Ext.MessageBox.alert(TXT_common_hint,TXT_common_access_fail);
	});
	ds.load();

	BestvSearch.glb_mainPanel.add(grid);
	BestvSearch.drawMainFrame(TXT_app_name + ' - ' + TXT_user_title);

}

function reloadUsers() {
	ds.reload();
}

Ext.onReady(displayUserPage);